<template>
  <el-dialog :title="!dataForm.id ? '测评发起新增' : '测评发起修改'" :close-on-click-modal="false" :visible.sync="visible" @close="clearDataList()" width="800px" top="8vh">
    <el-container>
      <el-main style="max-height:65vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
          <el-form-item label="测评会议名称" prop="hdName">
            <el-input v-model="dataForm.hdName" placeholder="请输入会议名称" style="width:90%;"></el-input>
          </el-form-item>
          <el-form-item label="关联类型" prop="hdCppfs">
            <el-select v-model="dataForm.hdCppfs" placeholder="请选择关联类型" :disabled="control" @change="elChange" style="width: 90%;">
              <el-option label="单票" value="1"></el-option>
              <el-option label="部门" value="2"></el-option>
              <el-option label="个人" value="3"></el-option>
              <el-option label="部门与个人" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="会议开始时间" prop="hdKssj">
            <el-date-picker style="width: 90%;" v-model="dataForm.hdKssj" type="datetime" placeholder="请选择会议开始时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
          <el-form-item label="会议结束时间" prop="hdJssj">
            <el-date-picker style="width: 90%;" v-model="dataForm.hdJssj" type="datetime" placeholder="请选择会议结束时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
          <el-form-item label="是否联合出票a" prop="hdSflhcp">
            <el-radio v-model="dataForm.hdSflhcp" label="1" disabled="true">是</el-radio>
            <el-radio v-model="dataForm.hdSflhcp" label="0" disabled="true">否</el-radio>
          </el-form-item>
          <el-form-item label="测评会议说明" prop="hdSm">
            <el-input v-model="dataForm.hdSm" type="textarea" :rows="3" placeholder="请输入会议说明" style="width:90%;"></el-input>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/ceping/eval-activity'
import { selhdidList } from '@/api/ceping/eval-activity-sheet'

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      control: false,
      lhcpAble: false,
      form: [],
      dataForm: {
        id: 0,
        hdName: '',
        hdSm: '',
        hdCppfs: '',
        hdKssj: '',
        hdJssj: '',
        hdSflhcp: '0',
      },
      dataRule: {
        hdName: [
          { required: true, message: '测试会议名称不能为空', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ],
        hdCppfs: [
          { required: true, message: '关联类型不能为空', trigger: 'blur' }
        ],
        hdKssj: [
          { required: true, message: '会议开始时间不能为空', trigger: 'blur' }
        ],
        hdJssj: [
          { required: true, message: '会议结束时间不能为空', trigger: 'blur' }
        ],
        hdSflhcp: [
          { required: true, message: '是否联合出票不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.control = false;
      this.lhcpAble = false;

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data;
            this.dataForm.hdSflhcp = response.data.data.hdSflhcp + "";
            this.control = (this.dataForm.sheetSum > 0) ;
            if(this.dataForm.hdCppfs === '单票'){
              this.lhcpAble = true;
            }
          });
          // selhdidList(this.dataForm.id).then(response => {
          //   if (response.data.data.length === 0) {
          //     this.control = false;
          //   } else {
          //     this.control = true;
          //   }
          // });
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.hdKssj > this.dataForm.hdJssj) {
            this.$notify.closeAll();
            this.$notify.error("开始时间不能大于结束时间。");
            return;
          }
          if (this.dataForm.hdJssj <= this.$moment(new Date()).format("YYYY-MM-DD hh:mm:ss")) {
            this.$notify.closeAll();
            this.$notify.error("会议结束时间不能小于当前时间。");
            return;
          }

          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.$emit('refreshList');
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('添加成功');
              this.visible = false;
              this.$emit('refreshList');
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    clearDataList() {
      this.dataForm = {
        id: 0,
        hdName: '',
        hdSm: '',
        hdCppfs: '',
        hdKssj: '',
        hdJssj: '',
        hdSflhcp: '0',
      };
    },
    //单票-联合售票 是
    elChange(value){
      if(value === '单票'){
        this.dataForm.hdSflhcp= '1';
        this.lhcpAble = true;
      }
      else{
        this.lhcpAble = false;
      }
    }

  }
}
</script>
